package com.sea.supply.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;

import com.codecrane.core.util.ProduceStatusEnum;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;

import com.sea.supply.entity.ProduceDetail;
import com.sea.supply.service.ProduceDetailService;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/produceDetail")
public class ProduceDetailController {

    @Autowired
    private ProduceDetailService produceDetailService;


    /**
     * 分页条件查询
     *
     * @param produceId
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByProduceId")
    public List<ProduceDetail> findByProduceId(Integer produceId) {
        return produceDetailService.findByProduceId(produceId);
    }
    /**
     * 修改
     * @param produceDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(ProduceDetail produceDetail) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produceDetail && produceDetailService.usave(produceDetail) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 关单
     * @param produceDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateFlag", method = RequestMethod.POST)
    public AjaxReturn updateFlag(ProduceDetail produceDetail) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produceDetail && produceDetailService.updateFlag(produceDetail) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 导出当前数据
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response, @RequestParam("ids") Integer ids[]) {
        List<ProduceDetail> list=produceDetailService.exportProduceDetail(ids);
        String[] title = {"采购单号","供应商","状态","渠道","SKU","产品标","产品名称","颜色","尺码","尺码(INCH)",
                "含税价","不含税价","采购总量","采购剩余","预计交期","合同号","装箱量","箱数","总金额","下单时间","币种","合同金额","增色单","账期","付款方式","短装数量"};
        String fileName = "采购明细表" + DateUtils.dateToString(new Date(),"yyyyMMdd") + ".xls";
        String sheetName = "采购明细表";
        String[][] content = new String[list.size()][26];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            ProduceDetail obj = list.get(i);
            content[i][0] = obj.getProduceNo();
            content[i][1] = obj.getProduceSupplierName();
            content[i][2] = ProduceStatusEnum.getStatus(obj.getProduceStatus());
            content[i][3] = obj.getOrderChannelName();
            content[i][4] = obj.getOrderSku();
            content[i][5] = obj.getRelatedFile();
            content[i][6] = obj.getProductName();
            content[i][7] = obj.getProductColorName();
            content[i][8] = obj.getProductSizeName();
            content[i][9] = obj.getProductInch();
            content[i][10] = obj.getProducePrice()+"";
            content[i][11] = obj.getProduceUnitPrice()+"";
            content[i][12] = obj.getProduceTotal()+"";
            content[i][13] = obj.getProduceSurplus()+"";
            content[i][14] = obj.getBuyOrderDate();
            content[i][15] = obj.getProduceAgreement();
            content[i][16] = obj.getProductPackageNumber()+"";
            content[i][17] = obj.getBoxNumber()+"";
            content[i][18] = obj.getProduceMoney()+"";
            content[i][19] = obj.getCreateTime()+"";
            content[i][20] = obj.getProduceMoneyType()+"";
            content[i][21] = obj.getTotalMoney()+"";
            if(null==obj.getProduceAddColor() || obj.getProduceAddColor()==0){
                content[i][22] = "否";
            }else {
                content[i][22] = "是";
            }
            content[i][23] = obj.getSupplierDay()+"";
            content[i][24] = obj.getPaymentType()+"";
            content[i][25] = obj.getProduceShort()+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
